package com.monlyu.action.user;

import java.util.Set;
import java.util.Map.Entry;

import com.monlyu.action.BaseAction;
import com.monlyu.bean.UserTree;
import com.monlyu.bo.UserBo;
import com.monlyu.util.ActionUtil;
import com.monlyu.vo.User;

/********************************************************************************
 * 
 * <b>功能描述☞</b>组装用户新系树<br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.action.user.UserTreeAction.java<br>
 * <b>开发日期☞</b>2010-2-28 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
public class UserTreeAction extends BaseAction {

	private UserBo userBo;
	private boolean multiple;
	private String list;
	private String htmlTree;

	public String getHtmlTree() {
		return htmlTree;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}

	public void setHtmlTree(String htmlTree) {
		this.htmlTree = htmlTree;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	@Override
	public String execute() throws Exception {
		decType = ActionUtil.SITEMESH_BODY;
		htmlTree = getTree().toString();
		return view("userTree.jsp");
	}

	private StringBuilder getTree() {
		int loop = 0;
		StringBuilder builder = new StringBuilder();
		Set<Entry<Long, UserTree>> entities = userBo.buildTree().entrySet();
		for (Entry<Long, UserTree> entry : entities) {
			builder.append(show(entry.getValue(), loop));
			loop++;
		}
		return builder;
	}

	public String show(UserTree ut, int loop) {
		StringBuilder tree = new StringBuilder();
		tree.append("<li><span class='dept'>");
		tree.append(ut.getDept());
		if (multiple) {
			tree.append("<input type='checkbox' name='selectDept' value='inDept" + loop + "'>");
		}
		tree.append("</span><ul id='inDept" + loop + "'>");
		for (User u : ut.getUsers()) {
			tree.append("<li>");
			tree.append("<span class='"+ut.getDept()+"' id='u" + u.getId() + "'>");
			tree.append(u.getName());
			tree.append("<input type='");
			tree.append(multiple ? "checkbox" : "radio");
			tree.append("' name='users' value='" + u.getId() + "' />");
			tree.append("</span></li>");
		}
		tree.append("</ul></li>");
		return tree.toString();
	}
}